package xcmg.device.service.purchase;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.basic.MaterialsPurchaseInfo;
import xcmg.device.dao.mapper.basic.MaterialsPurchaseInfoMapper;
import xcmg.device.service.vo.purchase.BuyPlanDetailVO;
import yb.ecp.fast.infra.infra.log.LogHelper;

import java.util.List;

@Service
public class BuyPlanUpdateService {

    @Autowired
    private MaterialsPurchaseInfoMapper materialsPurchaseInfoMapper;

    @Async
    public void saveOrUpdatePurchaseInfo(List<BuyPlanDetailVO> listVO, String orgId){
        if (CollectionUtils.isEmpty(listVO)) {
            return;
        }
        for (BuyPlanDetailVO detailVO : listVO) {
            if (CollectionUtils.isEmpty(detailVO.getPurchaseInfoVOList())) {
                continue;
            }
            materialsPurchaseInfoMapper.delete(orgId, detailVO.getMaterialsNo());
            for (MaterialsPurchaseInfo purchaseInfo : detailVO.getPurchaseInfoVOList()) {
                try {
                    purchaseInfo.setOrgId(orgId);
                    materialsPurchaseInfoMapper.insert(purchaseInfo);
                } catch (Exception e) {
                    LogHelper.monitor(e.getMessage());
                }
            }
        }

    }
}
